﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using FileManager_Client.BusinessLogic;
using FileManager_Client.BusinessLogic.FileManagerUserService;
using FileManager_Client.PresentationLayer.NewUi;
using FileManager_Client.Utility;
using System.Linq;

namespace FileManager_Client.PresentationLayer
{
    public partial class RightsManagement : Form
    {
        private string _folderSid;
        private FileService _fileService;
        private WaitDialogForm _waitBeforeLogin;
        private static object lockWait = new object();
        private UserLoginResult userSession;
        private bool _isPermissionDelegate;
        public RightsManagement(string folderSid,NodeArgs nodeArgs,bool isPermissionDelegate=false)
        {
            InitializeComponent();
            _isPermissionDelegate = isPermissionDelegate;
            _folderSid = folderSid;
            _fileService = new FileService();
            olvSimple.Items.Clear();
            userSession = CacheGlobal.GetCache(GlobalString.System_Config_String.UserSession) as UserLoginResult;
            if (!isPermissionDelegate) return;
            Text = "权限转授";
            btnAddOrganization.Enabled = false;
            btnAddPeopleGroup.Enabled = false;
            btnAddOrganizationGroup.Enabled = false;
            btnAnyOne.Enabled = false;
        }

        /// <summary>
        /// Add Company
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            var addCompany = new AddOrganization();
            addCompany.Closed += addCompany_Closed;
            addCompany.ShowDialog();
        }

        void addCompany_Closed(object sender, EventArgs e)
        {
            var addCompanyForm = sender as AddOrganization;
            if(null == addCompanyForm || addCompanyForm.ChooseCompany.Count <= 0) return;
            try
            {
                var objCollection = olvSimple.Objects as IEnumerable<FileRoleAddResult>;
                var hasCollection = addCompanyForm.ChooseCompany.Select(x => x.Idk__BackingField).ToList();
                if (objCollection != null && objCollection.Any())
                {
                    var delCollection =
                        objCollection.Where(
                            x =>
                            x.RoleTypek__BackingField.Equals("1") && !x.IsInheritk__BackingField && hasCollection.Contains(x.RelationIdk__BackingField))
                            .Select(x => x.RelationIdk__BackingField).ToList();
                    var newRoleCollection =
                        addCompanyForm.ChooseCompany.Where(x => !delCollection.Contains(x.Idk__BackingField));
                    if (newRoleCollection.Count() > 0)
                    {
                        var backgroundWokerAddEmployee = new BackgroundWorker();
                        backgroundWokerAddEmployee.DoWork += backgroundWokerAddEmployee_DoWork;
                        var args = new NewFileRoleCollectionArgs();
                        var argsObject = newRoleCollection.Select(userResult => new FileRoleAddResult
                        {
                            FileSidk__BackingField = _folderSid,
                            InheritFileSIdk__BackingField = "",
                            IsCreatek__BackingField = false,
                            IsUpdatek__BackingField = false,
                            IsDeletek__BackingField = false,
                            IsReadk__BackingField = true,
                            IsJustMek__BackingField = false,
                            IsInheritk__BackingField = false,
                            SetUserIdk__BackingField = userSession.UserIdk__BackingField,
                            SetUserNamek__BackingField = userSession.PeopleNamek__BackingField,
                            RelationIdk__BackingField = userResult.Idk__BackingField,
                            RelationNamek__BackingField = userResult.ShortNamek__BackingField,
                            RoleTypek__BackingField = "1"
                        }).ToList();
                        args.NewFileRoleCollectionk__BackingField = argsObject.ToArray();
                        backgroundWokerAddEmployee.RunWorkerAsync(args);
                        lock (lockWait)
                        {
                            _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.AddFolderRoleStr,
                                                                  GlobalString.System_Config_String.MbxInfoStr)
                            {
                                BackColor = BackColor
                            };
                            _waitBeforeLogin.Hide();
                            _waitBeforeLogin.ShowDialog();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("addCompany_Closed has problem", ex);
                XtraMessageBox.Show("添加权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Add Company Group
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            var addCompanyGroup = new AddCompanyGroup();
            addCompanyGroup.Closed += addCompanyGroup_Closed;
            addCompanyGroup.ShowDialog();
        }

        void addCompanyGroup_Closed(object sender, EventArgs e)
        {
            var addCompanyGroup = sender as AddCompanyGroup;
            if (null == addCompanyGroup || addCompanyGroup.ChooseCompanyGroup.Count <= 0) return;
            try
            {
                var objCollection = olvSimple.Objects as IEnumerable<FileRoleAddResult>;
                var hasCollection = addCompanyGroup.ChooseCompanyGroup.Select(x => x.Sidk__BackingField).ToList();
                if (objCollection != null && objCollection.Any())
                {
                    var delCollection =
                        objCollection.Where(
                            x =>
                            x.RoleTypek__BackingField.Equals("3") && !x.IsInheritk__BackingField && hasCollection.Contains(x.RelationIdk__BackingField))
                            .Select(x => x.RelationIdk__BackingField).ToList();
                    var newRoleCollection =
                        addCompanyGroup.ChooseCompanyGroup.Where(x => !delCollection.Contains(x.Sidk__BackingField));
                    if (newRoleCollection.Count() > 0)
                    {
                        var backgroundWokerAddEmployee = new BackgroundWorker();
                        backgroundWokerAddEmployee.DoWork += backgroundWokerAddEmployee_DoWork;
                        var args = new NewFileRoleCollectionArgs();
                        var argsObject = newRoleCollection.Select(userResult => new FileRoleAddResult
                        {
                            FileSidk__BackingField = _folderSid,
                            InheritFileSIdk__BackingField = "",
                            IsCreatek__BackingField = false,
                            IsUpdatek__BackingField = false,
                            IsDeletek__BackingField = false,
                            IsReadk__BackingField = true,
                            IsJustMek__BackingField = false,
                            IsInheritk__BackingField = false,
                            SetUserIdk__BackingField = userSession.UserIdk__BackingField,
                            SetUserNamek__BackingField = userSession.PeopleNamek__BackingField,
                            RelationIdk__BackingField = userResult.Sidk__BackingField,
                            RelationNamek__BackingField = userResult.Namek__BackingField,
                            RoleTypek__BackingField = "3"
                        }).ToList();
                        args.NewFileRoleCollectionk__BackingField = argsObject.ToArray();
                        backgroundWokerAddEmployee.RunWorkerAsync(args);
                        lock (lockWait)
                        {
                            _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.AddFolderRoleStr,
                                                                  GlobalString.System_Config_String.MbxInfoStr)
                            {
                                BackColor = BackColor
                            };
                            _waitBeforeLogin.Hide();
                            _waitBeforeLogin.ShowDialog();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("addCompanyGroup_Closed has problem", ex);
                XtraMessageBox.Show("添加权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Add User Group
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            var addUserGroup = new AddPeopleGroup();
            addUserGroup.Closed += addUserGroup_Closed;
            addUserGroup.ShowDialog();
        }

        void addUserGroup_Closed(object sender, EventArgs e)
        {
            var addUserGroup = sender as AddPeopleGroup;
            if (null == addUserGroup || addUserGroup.ChooseUserGroup.Count <= 0) return;
            try
            {
                var objCollection = olvSimple.Objects as IEnumerable<FileRoleAddResult>;
                var hasCollection = addUserGroup.ChooseUserGroup.Select(x => x.Sidk__BackingField).ToList();
                if (objCollection != null && objCollection.Any())
                {
                    var delCollection =
                        objCollection.Where(
                            x =>
                            x.RoleTypek__BackingField.Equals("2") && !x.IsInheritk__BackingField && hasCollection.Contains(x.RelationIdk__BackingField))
                            .Select(x => x.RelationIdk__BackingField).ToList();
                    var newRoleCollection =
                        addUserGroup.ChooseUserGroup.Where(x => !delCollection.Contains(x.Sidk__BackingField));
                    if (newRoleCollection.Count() > 0)
                    {
                        var backgroundWokerAddEmployee = new BackgroundWorker();
                        backgroundWokerAddEmployee.DoWork += backgroundWokerAddEmployee_DoWork;
                        var args = new NewFileRoleCollectionArgs();
                        var argsObject = newRoleCollection.Select(userResult => new FileRoleAddResult
                        {
                            FileSidk__BackingField = _folderSid,
                            InheritFileSIdk__BackingField = "",
                            IsCreatek__BackingField = false,
                            IsUpdatek__BackingField = false,
                            IsDeletek__BackingField = false,
                            IsReadk__BackingField = true,
                            IsJustMek__BackingField = false,
                            IsInheritk__BackingField = false,
                            SetUserIdk__BackingField = userSession.UserIdk__BackingField,
                            SetUserNamek__BackingField = userSession.PeopleNamek__BackingField,
                            RelationIdk__BackingField = userResult.Sidk__BackingField,
                            RelationNamek__BackingField = userResult.Namek__BackingField,
                            RoleTypek__BackingField = "2"
                        }).ToList();
                        args.NewFileRoleCollectionk__BackingField = argsObject.ToArray();
                        backgroundWokerAddEmployee.RunWorkerAsync(args);
                        lock (lockWait)
                        {
                            _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.AddFolderRoleStr,
                                                                  GlobalString.System_Config_String.MbxInfoStr)
                            {
                                BackColor = BackColor
                            };
                            _waitBeforeLogin.Hide();
                            _waitBeforeLogin.ShowDialog();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("addUserGroup_Closed has problem", ex);
                XtraMessageBox.Show("添加权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// Add Any One
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAnyOne_Click(object sender, EventArgs e)
        {
            try
            {
                var objCollection = olvSimple.Objects as IEnumerable<FileRoleAddResult>;
                if (objCollection != null && objCollection.Any())
                {
                    var delCollection =
                        objCollection.Where(
                            x =>
                            x.RoleTypek__BackingField.Equals("5") && !x.IsInheritk__BackingField).ToList();
                    if(delCollection.Count > 0) return;
                        var backgroundWokerAddEmployee = new BackgroundWorker();
                        backgroundWokerAddEmployee.DoWork += backgroundWokerAddEmployee_DoWork;
                        var args = new NewFileRoleCollectionArgs();
                    args.NewFileRoleCollectionk__BackingField = new FileRoleAddResult[]
                                                                    {
                                                                        new FileRoleAddResult
                                                                            {
                                                                                FileSidk__BackingField = _folderSid,
                                                                                InheritFileSIdk__BackingField = "",
                                                                                IsCreatek__BackingField = false,
                                                                                IsUpdatek__BackingField = false,
                                                                                IsDeletek__BackingField = false,
                                                                                IsReadk__BackingField = true,
                                                                                IsJustMek__BackingField = false,
                                                                                IsInheritk__BackingField = false,
                                                                                SetUserIdk__BackingField =
                                                                                    userSession.UserIdk__BackingField,
                                                                                SetUserNamek__BackingField =
                                                                                    userSession.
                                                                                    PeopleNamek__BackingField,
                                                                                RelationIdk__BackingField = "AnyOne",
                                                                                RelationNamek__BackingField = "AnyOne",
                                                                                RoleTypek__BackingField = "5"
                                                                            }
                                                                    };
                        backgroundWokerAddEmployee.RunWorkerAsync(args);
                        lock (lockWait)
                        {
                            _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.AddFolderRoleStr,
                                                                  GlobalString.System_Config_String.MbxInfoStr)
                            {
                                BackColor = BackColor
                            };
                            _waitBeforeLogin.Hide();
                            _waitBeforeLogin.ShowDialog();
                        }
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("btnAnyOne_Click has problem", ex);
                XtraMessageBox.Show("添加权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            //var test = new FileRole
            //               {
            //                   TypeName = "555",
            //                   IsCreate = false,
            //                   IsUpdate = true,
            //                   IsInner = "否",
            //                   IsRead = false,
            //                   HideType = "家庭"
            //               };
            //olvSimple.AddObject(test);
            //var test11 = olvSimple.Items;
            //var fileRoles = new List<FileRole>();
            //foreach (var o in olvSimple.Objects)
            //{
            //    fileRoles.Add(o as FileRole);
            //}
            //var tsss = fileRoles;
        }

        private void btnAddPeople_Click(object sender, EventArgs e)
        {
            if(_isPermissionDelegate)
            {
                var addMydepartmentPeople = new AddMyDepartmentPeople();
                addMydepartmentPeople.Closed += addMydepartmentPeople_Closed;
                addMydepartmentPeople.ShowDialog();
            }
            else
            {
                var addEmployee = new AddEmployee();
                addEmployee.Closed += addEmployee_Closed;
                addEmployee.ShowDialog();
            }
        }

        void addMydepartmentPeople_Closed(object sender, EventArgs e)
        {
            var addEmployeeForm = sender as AddMyDepartmentPeople;
            if (null == addEmployeeForm || addEmployeeForm.ChoosePeople.Count <= 0) return;
            try

            {
                var objCollection = olvSimple.Objects as IEnumerable<FileRoleAddResult>;
                var hasCollection = addEmployeeForm.ChoosePeople.Select(x => x.UserIdk__BackingField).ToList();
                if (objCollection != null && objCollection.Any())
                {
                    var delCollection =
                        objCollection.Where(
                            x =>
                            x.RoleTypek__BackingField.Equals("0") && !x.IsInheritk__BackingField && hasCollection.Contains(x.RelationIdk__BackingField))
                            .Select(x => x.RelationIdk__BackingField).ToList();
                    var newRoleCollection =
                        addEmployeeForm.ChoosePeople.Where(x => !delCollection.Contains(x.UserIdk__BackingField));
                    if (newRoleCollection.Count() > 0)
                    {
                        var backgroundWokerAddEmployee = new BackgroundWorker();
                        backgroundWokerAddEmployee.DoWork += backgroundWokerAddEmployee_DoWork;
                        var args = new NewFileRoleCollectionArgs();
                        var argsObject = newRoleCollection.Select(userResult => new FileRoleAddResult
                        {
                            FileSidk__BackingField = _folderSid,
                            InheritFileSIdk__BackingField = "",
                            IsCreatek__BackingField = false,
                            IsUpdatek__BackingField = false,
                            IsDeletek__BackingField = false,
                            IsReadk__BackingField = true,
                            IsJustMek__BackingField = false,
                            IsInheritk__BackingField = false,
                            SetUserIdk__BackingField = userSession.UserIdk__BackingField,
                            SetUserNamek__BackingField = userSession.PeopleNamek__BackingField,
                            RelationIdk__BackingField = userResult.UserIdk__BackingField,
                            RelationNamek__BackingField = userResult.UserNamek__BackingField,
                            RoleTypek__BackingField = "0"
                        }).ToList();
                        args.NewFileRoleCollectionk__BackingField = argsObject.ToArray();
                        backgroundWokerAddEmployee.RunWorkerAsync(args);
                        lock (lockWait)
                        {
                            _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.AddFolderRoleStr,
                                                                  GlobalString.System_Config_String.MbxInfoStr)
                            {
                                BackColor = BackColor
                            };
                            _waitBeforeLogin.Hide();
                            _waitBeforeLogin.ShowDialog();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("addMydepartmentPeople_Closed has problem", ex);
                XtraMessageBox.Show("添加权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        void addEmployee_Closed(object sender, EventArgs e)
        {
            var addEmployeeForm = sender as AddEmployee;
            if (null == addEmployeeForm ||  addEmployeeForm.ChooseUser.Count <=0) return;
            try
            {
                var objCollection = olvSimple.Objects as IEnumerable<FileRoleAddResult>;
                var hasCollection = addEmployeeForm.ChooseUser.Select(x => x.UserIdk__BackingField).ToList();
                if(objCollection != null && objCollection.Any())
                {
                    var delCollection =
                        objCollection.Where(
                            x =>
                            x.RoleTypek__BackingField.Equals("0") &&!x.IsInheritk__BackingField&& hasCollection.Contains(x.RelationIdk__BackingField))
                            .Select(x => x.RelationIdk__BackingField).ToList();
                    var newRoleCollection =
                        addEmployeeForm.ChooseUser.Where(x => !delCollection.Contains(x.UserIdk__BackingField));
                    if(newRoleCollection.Count()>0)
                    {
                        var backgroundWokerAddEmployee = new BackgroundWorker();
                        backgroundWokerAddEmployee.DoWork += backgroundWokerAddEmployee_DoWork;
                        var args = new NewFileRoleCollectionArgs();
                        var argsObject = newRoleCollection.Select(userResult => new FileRoleAddResult
                        {
                            FileSidk__BackingField = _folderSid,
                            InheritFileSIdk__BackingField = "",
                            IsCreatek__BackingField = false,
                            IsUpdatek__BackingField = false,
                            IsDeletek__BackingField = false,
                            IsReadk__BackingField = true,
                            IsJustMek__BackingField = false,
                            IsInheritk__BackingField = false,
                            SetUserIdk__BackingField = userSession.UserIdk__BackingField,
                            SetUserNamek__BackingField = userSession.PeopleNamek__BackingField,
                            RelationIdk__BackingField = userResult.UserIdk__BackingField,
                            RelationNamek__BackingField = userResult.UserNamek__BackingField,
                            RoleTypek__BackingField = "0"
                        }).ToList();
                        args.NewFileRoleCollectionk__BackingField = argsObject.ToArray();
                        backgroundWokerAddEmployee.RunWorkerAsync(args);
                        lock (lockWait)
                        {
                            _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.AddFolderRoleStr,
                                                                  GlobalString.System_Config_String.MbxInfoStr)
                            {
                                BackColor = BackColor
                            };
                            _waitBeforeLogin.Hide();
                            _waitBeforeLogin.ShowDialog();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("addEmployee_Closed has problem", ex);
                XtraMessageBox.Show("添加权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        void backgroundWokerAddEmployee_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var args = e.Argument as NewFileRoleCollectionArgs;
                var reslut = _fileService.CreateNewFileRole(args);
                Invoke((EventHandler)delegate
                {
                    _waitBeforeLogin.Close();
                });
                if (reslut.Resultk__BackingField.Count() > 0)
                {
                    if (!IsHandleCreated)
                        CreateHandle();
                    Invoke((EventHandler)delegate
                    {
                        //var obj = olvSimple.SelectedObject as FileRoleAddResult;
                        //if (null == obj) return;
                        //var objCollection = args.NewFileRoleCollectionk__BackingField.ToList();
                        //var serverObjCollection = reslut.Resultk__BackingField.ToList();
                        //foreach (var temp in objCollection)
                        //{
                        //    var tempObj =
                        //        serverObjCollection.FirstOrDefault(
                        //            x => x.RelationIdk__BackingField.Equals(temp.RelationIdk__BackingField));
                        //    if (tempObj == null) continue;
                        //    temp.FlagSidk__BackingField = tempObj.FlagIdk__BackingField;
                        //    temp.RoleSidk__BackingField = tempObj.RoleIdk__BackingField;
                        //    temp.RoleTypeNamek__BackingField = RoleTypeName(temp.RoleTypek__BackingField);
                        //    olvSimple.AddObject(temp);
                        //}
                        //olvSimple.Refresh();
                        olvSimple.ClearObjects();
                        olvSimple.Items.Clear();
                        RightsManagement_Shown(this, null);
                    });
                }
                else
                {
                    var message = string.Empty;
                    switch (reslut.ResultExceptionk__BackingField)
                    {
                        case BaseExceptionEnum.ChangeFileRoleException:
                            message = GlobalString.System_Exception_String.GetFolderRoleExceptionStr;
                            break;
                    }
                    if (!string.IsNullOrEmpty(message))
                        XtraMessageBox.Show(message, GlobalString.System_Exception_String.MbxErrorStr,
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                }

            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("backgroundWokerChangeRole_DoWork has problem", ex);
                XtraMessageBox.Show("修改文件夹权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                Invoke((EventHandler)delegate
                {
                    Close();
                });
            }
        }

        private void cms_LvDelete_Opening(object sender, CancelEventArgs e)
        {
            var obj = olvSimple.SelectedObject as FileRoleAddResult;
            tsmi_ChangeRole.Enabled = true;
            if (null == obj)
            {
                e.Cancel = true;
                return;
            }
            if (obj.IsInheritk__BackingField)
                tsmi_ChangeRole.Enabled = false;
            if (null == olvSimple.SelectedItem)
                e.Cancel = true;
            else
                tsmi_DeleteAuth.Text = string.Format("删除 {0} 的权限", olvSimple.SelectedItem.Text);
        }

        private void tsmi_ChangeRole_Click(object sender, EventArgs e)
        {
          
            var obj = olvSimple.SelectedObject as FileRoleAddResult;
            if (null == obj) return;
            var changeRole = new ChangeRole(obj.IsDeletek__BackingField,
                obj.IsCreatek__BackingField, obj.IsJustMek__BackingField, obj.IsReadk__BackingField
                , obj.IsUpdatek__BackingField);
            changeRole.Closed += changeRole_Closed;
            changeRole.ShowDialog();
        }

        private void olvSimple_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right) return;
            var obj = olvSimple.SelectedObject as FileRoleAddResult;
            if (null == obj) return;
            if (obj.IsInheritk__BackingField) return;
            var changeRole = new ChangeRole(obj.IsDeletek__BackingField,
                obj.IsCreatek__BackingField, obj.IsJustMek__BackingField, obj.IsReadk__BackingField
                , obj.IsUpdatek__BackingField);
            changeRole.Closed += changeRole_Closed;
            changeRole.ShowDialog();
        }

        void changeRole_Closed(object sender, EventArgs e)
        {
            var changeRole = sender as ChangeRole;
            var obj = olvSimple.SelectedObject as FileRoleAddResult;
            if (null == obj) return;
            if (null == changeRole) return;
            if (!changeRole.IsSave) return;
            try
            {
                var backgroundWokerChangeRole = new BackgroundWorker();
                backgroundWokerChangeRole.DoWork += backgroundWokerChangeRole_DoWork;
                var fileRoleChangeArgs = new FileRoleChangeArgs
                                             {
                                                 IsCreatek__BackingField = changeRole.IsCreate,
                                                 IsDeletek__BackingField = changeRole.IsDelete,
                                                 IsJustMek__BackingField = changeRole.IsJustMe,
                                                 IsReadk__BackingField = changeRole.IsRead,
                                                 IsUpdatek__BackingField = changeRole.IsUpdate,
                                                 UpdateSubFolderk__BackingField = changeRole.UpdateSubFolder,
                                                 RoleSidk__BackingField = obj.RoleSidk__BackingField
                                             };
                backgroundWokerChangeRole.RunWorkerAsync(fileRoleChangeArgs);
                lock (lockWait)
                {
                    _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.ChangeFolderRoleStr,
                                                          GlobalString.System_Config_String.MbxInfoStr)
                    {
                        BackColor = BackColor
                    };
                    _waitBeforeLogin.Hide();
                    _waitBeforeLogin.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("changeRole_Closed has problem", ex);
                XtraMessageBox.Show("修改文件夹权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        void backgroundWokerChangeRole_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var args = e.Argument as FileRoleChangeArgs;
                var reslut = _fileService.ChangeFileRole(args);
                Invoke((EventHandler)delegate
                {
                    _waitBeforeLogin.Close();
                });
                if (reslut.Resultk__BackingField)
                {
                    if(!IsHandleCreated)
                        CreateHandle();
                    Invoke((EventHandler)delegate
                    {
                        var obj = olvSimple.SelectedObject as FileRoleAddResult;
                        if (null == obj) return;
                        obj.IsCreatek__BackingField = args.IsCreatek__BackingField;
                        obj.IsDeletek__BackingField = args.IsDeletek__BackingField;
                        obj.IsUpdatek__BackingField = args.IsUpdatek__BackingField;
                        obj.IsReadk__BackingField = args.IsReadk__BackingField;
                        obj.IsJustMek__BackingField = args.IsJustMek__BackingField;
                    });
                }
                else
                {
                    var message = string.Empty;
                    switch (reslut.ResultExceptionk__BackingField)
                    {
                        case BaseExceptionEnum.ChangeFileRoleException:
                            message = GlobalString.System_Exception_String.GetFolderRoleExceptionStr;
                            break;
                    }
                    if (!string.IsNullOrEmpty(message))
                        XtraMessageBox.Show(message, GlobalString.System_Exception_String.MbxErrorStr,
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                }

            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("backgroundWokerChangeRole_DoWork has problem", ex);
                XtraMessageBox.Show("修改文件夹权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                Invoke((EventHandler)delegate
                {
                    Close();
                });
            }
        }

        private void RightsManagement_Shown(object sender, EventArgs e)
        {
            try
            {
                var backgroundWorker = new BackgroundWorker();
                backgroundWorker.DoWork += backgroundWorker_DoWork;
                var folerArgs = new FileRoleAddArgs
                                    {
                                        FileSidk__BackingField = _folderSid
                                    };
                backgroundWorker.RunWorkerAsync(folerArgs);
                lock (lockWait)
                {
                    _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.GetFolderRoleStr,
                                                          GlobalString.System_Config_String.MbxInfoStr)
                    {
                        BackColor = BackColor
                    };
                    _waitBeforeLogin.Hide();
                    _waitBeforeLogin.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("RightsManagement_Shown has problem", ex);
                XtraMessageBox.Show("获取文件夹权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var folderArgs = e.Argument as FileRoleAddArgs;
                var reslut = _fileService.GetFoleRoleCollection(folderArgs);
                if(!IsHandleCreated)
                    CreateHandle();
                Invoke((EventHandler)delegate
                {
                    _waitBeforeLogin.Close();
                });
                if (reslut.Resultk__BackingField.Length > 0)
                {
                    foreach (var fileRoleAddResult in reslut.Resultk__BackingField)
                    {
                        fileRoleAddResult.RoleTypeNamek__BackingField =
                            RoleTypeName(fileRoleAddResult.RoleTypek__BackingField);
                        fileRoleAddResult.IsInheritNamek__BackingField =
                            !fileRoleAddResult.IsInheritk__BackingField ? "否" : "是";
                    }
                    olvSimple.AlwaysGroupByColumn = olvHideColumn;
                    olvSimple.SetObjects(reslut.Resultk__BackingField);
                //    olvSimple.Update();
                }
                else
                {
                    var message = string.Empty;
                    switch (reslut.ResultExceptionk__BackingField)
                    {
                        case BaseExceptionEnum.GetFolderRoleException:
                            message = GlobalString.System_Exception_String.GetFolderRoleExceptionStr;
                            break;
                    }
                    if (!string.IsNullOrEmpty(message))
                        XtraMessageBox.Show(message, GlobalString.System_Exception_String.MbxErrorStr,
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("GetFolderRole backgroundWorker_DoWork has problem", ex);
                XtraMessageBox.Show("获取文件夹权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                Invoke((EventHandler)delegate
                {
                    Close();
                });
            }
        }

        private string RoleTypeName(string roleType)
        {
            var result = string.Empty;
            switch (roleType)
            {
                //用户
                case "0":
                    result = GlobalString.System_Config_String.RoleTypeNameUserStr;
                    break;
                //组织机构
                case "1":
                    result = GlobalString.System_Config_String.RoleTypeNameCompanyStr;
                    break;
                //人员公共组
                case "2":
                    result = GlobalString.System_Config_String.RoleTypeNameUserGroupStr;
                    break;
                //组织机构公共组
                case "3":
                    result = GlobalString.System_Config_String.RoleTypeNameCompanyGroupStr;
                    break;
                //任何人
                case "5":
                    result = GlobalString.System_Config_String.RoleTypeNameAnyOneStr;
                    break;
            }

            return result;
        }

        private void tsmi_DeleteAuth_Click(object sender, EventArgs e)
        {
            var deleteSubFolderRole = XtraMessageBox.Show(GlobalString.System_Config_String.DeleteSubFolderRoleStr, GlobalString.System_Config_String.MbxInfoStr, MessageBoxButtons.YesNo,
                                        MessageBoxIcon.Information) == DialogResult.Yes;
            var obj = olvSimple.SelectedObject as FileRoleAddResult;
            if (null == obj) return;
            try
            {
                var backgroundWokerDeleteRole = new BackgroundWorker();
                backgroundWokerDeleteRole.DoWork += backgroundWokerDeleteRole_DoWork;
                var deleteFolderArgs = new DeleteFileRoleArgs
                                           {
                                               DeleteSubFolderRolek__BackingField = deleteSubFolderRole,
                                               FileRoleSidk__BackingField = obj.RoleSidk__BackingField,
                                               FileSidk__BackingField = obj.FileSidk__BackingField
                                           };
                backgroundWokerDeleteRole.RunWorkerAsync(deleteFolderArgs);
                lock (lockWait)
                {
                    _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.DeleteFolderRoleStr,
                                                          GlobalString.System_Config_String.MbxInfoStr)
                    {
                        BackColor = BackColor
                    };
                    _waitBeforeLogin.Hide();
                    _waitBeforeLogin.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("RightsManagement_Shown has problem", ex);
                XtraMessageBox.Show("删除文件夹权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        void backgroundWokerDeleteRole_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var deleteFolderRole = e.Argument as DeleteFileRoleArgs;
                var reslut = _fileService.DeleteFileRole(deleteFolderRole);
                if (!IsHandleCreated)
                    CreateHandle();
                if(reslut.Resultk__BackingField)
                {
                    Invoke((EventHandler)delegate
                    {
                        _waitBeforeLogin.Close();
                        olvSimple.ClearObjects();
                        olvSimple.Items.Clear();
                        RightsManagement_Shown(this, null);
                    });
                }
                else
                {
                    var message = string.Empty;
                    switch (reslut.ResultExceptionk__BackingField)
                    {
                        case BaseExceptionEnum.DeleteFileRoleException:
                            message = GlobalString.System_Exception_String.GetFolderRoleExceptionStr;
                            break;
                    }
                    if (!string.IsNullOrEmpty(message))
                        XtraMessageBox.Show(message, GlobalString.System_Exception_String.MbxErrorStr,
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("backgroundWokerDeleteRole_DoWork has problem", ex);
                XtraMessageBox.Show("删除文件夹权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                Invoke((EventHandler)delegate
                {
                    Close();
                });
            }
        }

        private void tsmi_AddRoleToSubFolder_Click(object sender, EventArgs e)
        {
            var obj = olvSimple.SelectedObject as FileRoleAddResult;
            if (null == obj) return;
            try
            {
                var backgroundWokderApplyRoleSubFolder = new BackgroundWorker();
                backgroundWokderApplyRoleSubFolder.DoWork += backgroundWokderApplyRoleSubFolder_DoWork;
                var applyRoleToSubFolderArgs = new ApplySubFolderRoleArgs
                                                   {
                                                       FileSidk__BackingField = obj.FileSidk__BackingField,
                                                       RoleSidk__BackingField = obj.RoleSidk__BackingField
                                                   };
                backgroundWokderApplyRoleSubFolder.RunWorkerAsync(applyRoleToSubFolderArgs);
                lock (lockWait)
                {
                    _waitBeforeLogin = new WaitDialogForm(GlobalString.System_Config_String.ApplyRoleToSubFolder,
                                                          GlobalString.System_Config_String.MbxInfoStr)
                    {
                        BackColor = BackColor
                    };
                    _waitBeforeLogin.Hide();
                    _waitBeforeLogin.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("RightsManagement_Shown has problem", ex);
                XtraMessageBox.Show("删除文件夹权限失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        void backgroundWokderApplyRoleSubFolder_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var applySubFolderRole = e.Argument as ApplySubFolderRoleArgs;
                var reslut = _fileService.ApplySubFolderRole(applySubFolderRole);
                if (!IsHandleCreated)
                    CreateHandle();
                if (reslut.Resultk__BackingField)
                {
                    Invoke((EventHandler)delegate
                    {
                        _waitBeforeLogin.Close();
                        olvSimple.ClearObjects();
                        olvSimple.Items.Clear();
                        RightsManagement_Shown(this, null);
                    });
                }
                else
                {
                    var message = string.Empty;
                    switch (reslut.ResultExceptionk__BackingField)
                    {
                        case BaseExceptionEnum.ApplySubFolderRoleExpcetion:
                            message = GlobalString.System_Exception_String.GetFolderRoleExceptionStr;
                            break;
                    }
                    if (!string.IsNullOrEmpty(message))
                        XtraMessageBox.Show(message, GlobalString.System_Exception_String.MbxErrorStr,
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("backgroundWokderApplyRoleSubFolder_DoWork has problem", ex);
                XtraMessageBox.Show("应用文件夹权限失败,请联系管理员!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                Invoke((EventHandler)delegate
                {
                    Close();
                });
            }
        }
    }

    //public class FileRole
    //{
    //    public string TypeName { get; set; }
    //    public bool IsJustMe { get; set; }
    //    public bool IsCreate { get; set; }
    //    public bool IsRead { get; set; }
    //    public bool IsUpdate { get; set; }
    //    public string IsInner { get; set; }
    //    public string IsInnerTo { get; set; }
    //    public string AuthorizeTo { get; set; }
    //    public string HideType { get; set; }
    //}
}
